package org.methodbox.extractor;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 *
 * @author Christian
 */
public class Stats {

    private BufferedWriter output;

    int totalStringKeys = 0;

    private void count(File inputFile) throws ExtractorException{
        try {
            Hashtable <String, Integer> counter = new Hashtable <String, Integer>();
            BufferedReader input = new BufferedReader (new FileReader(inputFile));
            while (input.ready()) {
                String val = input.readLine();
                Integer count = counter.get(val);
                if (count == null){
                    count = 1;
                } else {
                    count = count + 1;
                }
                counter.put(val, count);
            }
            output.write(inputFile.getName()+"\n");
            int blankCount = 0;
            int blankKeys = 0;
            int intCount = 0;
            int intKeys = 0;
            int negIntCount = 0;
            int negIntKeys = 0;
            int floatCount = 0;
            int floatKeys = 0;
            int stringCount = 0;
            int stringKeys = 0;
            String[] keys = counter.keySet().toArray(new String[0]);
            for (int i = 0; i< keys.length; i++) {
                if (keys[i].isEmpty() || keys[i].matches("^\\s*$")){
                    blankCount = blankCount + counter.get(keys[i]);
                    blankKeys++;
                } else if (keys[i].matches("^\\d*$")){
                    intCount = intCount + counter.get(keys[i]);
                    intKeys++;
                } else if (keys[i].matches("^-\\d*$")){
                    negIntCount = negIntCount + counter.get(keys[i]);
                    negIntKeys++;
                } else if (keys[i].matches("^-?\\d*\\.\\d*$")){
                    floatCount = floatCount + counter.get(keys[i]);
                    floatKeys++;
                } else {
                    stringCount = stringCount + counter.get(keys[i]);
                    stringKeys++;
                    totalStringKeys++;
                }
            }
            if (blankCount > 0) {
                output.write("\tBlanks: " + blankCount);
                if (blankKeys != 1) {
                    output.write(" over: " + blankKeys + " different formats.\n");
                } else {
                    output.write(" single format.\n");
                }
            }
            if (negIntCount > 0) {
                output.write("\tNegative Integers: " + negIntCount);
                if (negIntKeys != 1) {
                    output.write(" over: " + negIntKeys + " different values.\n");
                } else {
                    output.write(" 1 Value.\n");
                }
            }
            if (intCount > 0) {
                output.write("\tIntegers: " + intCount);
                if (intKeys != 1) {
                    output.write(" over: " + intKeys + " different values.\n");
                } else {
                    output.write(" 1 Value.\n");
                }
            }
            if (floatCount > 0) {
                output.write("\tFloats: " + floatCount);
                if (floatKeys > 0) {
                    output.write(" over: " + floatKeys + " different values.\n");
                } else {
                    output.write(" 1 Value.\n");
                }
            }
            if (stringCount > 0) {
                output.write("\tOther: " + stringCount);
                if (stringKeys != 1) {
                    output.write(" over: " + stringKeys + " different values.\n");
                } else {
                    output.write(" 1 Value.\n");
                }
            }
            if (counter.size() <= 10){
               for (int i = 0; i< keys.length; i++) {
                   output.write("\t"+keys[i]+":\t"+counter.get(keys[i])+"\n");
               }
            }
            output.write("\n\n");
        } catch (IOException ex) {
            throw new ExtractorException(ex);
        }
    }

    private String scrub(String name){
        String scrubbed = name.toLowerCase();
        scrubbed = scrubbed.replace("\"", "");
        return scrubbed;
    }

    private void openOutput(File inputDir) throws ExtractorException{
        try {
            File outputFile = new File (inputDir,"stats.info");
            output = new BufferedWriter(new FileWriter(outputFile));
        } catch (IOException ex) {
            throw new ExtractorException(ex);
        //} catch (FileNotFoundException ex) {
        //    throw new ExtractorException(ex);
        }
    }

    private void close() throws ExtractorException{
        try {
            output.write("totalStringKeys =" + totalStringKeys + "\n");
            output.close();
        } catch (IOException ex) {
             throw new ExtractorException(ex);
        }
    }

    public Stats(){
    }

    public void run(String inputDirName) throws ExtractorException{
        File inputDir = new File(inputDirName);
        File[] inputFiles = inputDir.listFiles();
        openOutput(inputDir);
        for (int i = 0; i< inputFiles.length; i++){
            if (inputFiles[i].isFile()){
                if (inputFiles[i].isFile()){
                    String fileName = inputFiles[i].getName();
                    if (fileName.endsWith(".txt")){
                        System.out.println(fileName);
                        count(inputFiles[i]);
                    }
                }
            }
        }
       close();
    }

    public static void main( String[] args ) throws ExtractorException
    {
        //tring inputFileName = "C:/Ruby/elab/java/extractor/Data/small.data";
        //tring columnsToPick = "Afford4,Amthb";
        String inputDirName = "C:/elabs/csv/hse08ai";
        Stats stats = new Stats();
        stats.run(inputDirName);
    }

}
